package com.fxsh168.server.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.fxsh168.server.adapters.MsgAdapter;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.interfaces.Constant;
import com.fxsh168.server.entitys.MyMsgQREntity;
import com.fxsh168.server.entitys.RegiYZMCG2;
import com.fxsh168.server.R;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class MessageCenter extends BaseActivity implements View.OnClickListener {
    private TextView mBtnClearMsg;
    private ListView mMsgListView;
    private List<MyMsgQREntity.DataBean> listData;//数据源
    private MsgAdapter mMsgAdapter;
    private String mMyMsgPhone, mMyMsgToken;
    PopupWindows_clear popupWindowsClear;
    LinearLayout liMessages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_center);
        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        mMyMsgPhone = sp.getString("mPhone", "");
        mMyMsgToken = sp.getString("token", "");
        getAllView();
        //创建页面就进行一次消息查询请求
        initMsgCheck();
        listData = new ArrayList<>();//先获得一个空数据源
        mMsgAdapter = new MsgAdapter(listData, MessageCenter.this);//初始化消息适配器
        mMsgListView.setAdapter(mMsgAdapter);//将listView和适配器关联起来
    }

    //创建页面就进行一次消息查询请求
    private void initMsgCheck() {
        OkHttpUtils.post().addParams("phone", mMyMsgPhone).addParams("token", mMyMsgToken)
                .url(Constant.MYMSGINFOHAVEDRIVERREQUESTPATH).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("onError", "onError=" + e);
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    List<MyMsgQREntity.DataBean> listData2 = new ArrayList<MyMsgQREntity.DataBean>();
                    MyMsgQREntity myMsgQREntity = new Gson().fromJson(response, MyMsgQREntity.class);
                    String codeSysMsg = myMsgQREntity.getCode().toString().trim();
                    if (codeSysMsg.equals(200 + "")) {
                        Object dataMsgInfo0 = myMsgQREntity.getData();
                        if (dataMsgInfo0 instanceof String) {
                            Log.e("shuju", "空数据2");
                        } else {
                            List<MyMsgQREntity.DataBean> dataMsgInfo = (List<MyMsgQREntity.DataBean>) dataMsgInfo0;
                            if (dataMsgInfo.size() > 0) {
                                listData.addAll(dataMsgInfo);
                                mMsgAdapter.notifyDataSetChanged();
                            }
                        }
                    } else if (codeSysMsg.equals(201 + "")) {
                        Log.e("shuju", "非法请求");
                    }
                }
            }
        });
    }

    //获得所有view
    private void getAllView() {
        liMessages = (LinearLayout) findViewById(R.id.liMessages);
        mBtnClearMsg = (TextView) findViewById(R.id.btnClearMsg);//清空view
        mMsgListView = (ListView) findViewById(R.id.msgListView);//获得listview
        mBtnClearMsg.setOnClickListener(this);

    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.btnClearMsg:
                popupWindowsClear = new PopupWindows_clear(MessageCenter.this, view);
                break;
        }
    }

    //清空消息操作
    private void initBtnClearMsg() {
        OkHttpUtils.post().addParams("phone", mMyMsgPhone).addParams("token", mMyMsgToken).url(Constant.MYMSGCLEARDRIVERREQUESTPATH).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                /*200  返回消息
                201 非法请求
                203 消息清除失败*/
                if (response != null) {
                    RegiYZMCG2 regiYZMCG2 = new Gson().fromJson(response, RegiYZMCG2.class);
                    String code = regiYZMCG2.getCode().trim();
                    if (code.equals(200 + "")) {
                        listData.clear();
                        mMsgAdapter.notifyDataSetChanged();
                    } else if (code.equals(201 + "")) {
                        Log.e("shuju", "非法请求！");
                    } else if (code.equals(203 + "")) {
                        showToast("消息清除失败!");
                    }
                }
            }
        });
    }

    //           清除消息
    public class PopupWindows_clear extends PopupWindow {
        View view;

        public PopupWindows_clear(Context mContext, View parent) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.popwin_clear, null);
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            setFocusable(true);
            setOutsideTouchable(true);
            setContentView(view);
            showAtLocation(parent, Gravity.CENTER, 0, 0);
            update();
            TextView teOkClear = (TextView) view.findViewById(R.id.teOkClear);
            TextView teCancel = (TextView) view.findViewById(R.id.teCalcel);
            teOkClear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    initBtnClearMsg();//清除消息
                    popupWindowsClear.dismiss();
                }
            });
            teCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupWindowsClear.dismiss();
                }
            });
        }
    }

}
